<template>
  <div class="app-container">
    <el-form :model="demo" label-width="80px" ref="demo" :rules='rules'>
      <el-row>
          <el-col :span="8">
          <el-form-item prop="company" label="公司">
            <el-input v-model="demo.company" icon="search" name="title" placeholder="请输入公司名称" required></el-input>
          </el-form-item>
        </el-col>
        </el-row>
        <el-row>
          <el-col :span="8">
          <el-form-item prop="displayName" label="负责人" label-width="80px">
            <el-input v-model="demo.displayName" icon="search" name="title" placeholder="请输入名字"></el-input>
          </el-form-item>
        </el-col>
        <el-col :span="8">
          <el-form-item prop="cellphone" label="手机号" label-width="80px">
            <el-input v-model="demo.cellphone" @blur="cellphoneTest" icon="search" type="number" name="title" placeholder="请输入手机号"></el-input>
          </el-form-item>
        </el-col>
        <el-col :span="8">
          <el-form-item prop="identityCard" label="身份证" label-width="80px">
            <el-input v-model="demo.identityCard" icon="search" @blur="identityCardTest" name="title"  placeholder="请输入手机号"></el-input>
          </el-form-item>
        </el-col>
        </el-row>
        <el-row>
          <el-col :span="8">
            <el-form-item prop="ord" label="属性">
              <el-input v-model="demo.ord" icon="search" name="title" placeholder="请输入内容" value="代理商" :disabled="true">代理商</el-input>
            </el-form-item>
          </el-col>
        </el-row>
        
        <el-row>
          <el-col :span="8">
          <el-form-item prop="province" label="授权区域" label-width="80px">
            <el-select v-model="demo.province" placeholder="请选择" style="width: 160px;" @change="cityShow">
              <el-option
                v-for="item in options"
                :key="item.value"
                :label="item.label"
                :value="item.value">
              </el-option>
            </el-select>
          </el-form-item>
        </el-col>
        <el-col :span="8">
          <el-form-item prop="city" label="城市" label-width="80px">
            <el-select v-model="demo.city" placeholder="请选择" style="width: 160px;" @change="countyShow">
              <el-option
                v-for="item in citys"
                :label="item.label"
                :value="item.value">
              </el-option>
            </el-select>
          </el-form-item>
        </el-col>
        </el-row>
        <el-row>
          <el-col :span="8">
          <el-form-item prop="activationMax" label="激活数量" label-width="80px">
            <el-input v-model="demo.activationMax" icon="search" name="title" type="number" placeholder="请输入数量"></el-input>
          </el-form-item>
        </el-col>
        <el-col :span="8">
          <el-form-item prop="years" label="服务时间" label-width="80px">
            <el-input v-model="demo.years" icon="search" name="title" value="number" type="number" placeholder="请输入月份数量"></el-input>
            <!-- <el-select v-model="demo.years" placeholder="请选择" style="width: 160px;">
              <el-option
                v-for="item in years"
                :key="item.value"
                :label="item.label"
                :value="item.value">
              </el-option>
            </el-select> -->
          </el-form-item>
        </el-col>
        </el-row>
          <el-form-item prop="checkedCities" label="授权方案" label-width="80px">
             <el-checkbox-group v-model="demo.checkedCities">
              <el-checkbox v-for="city in cities" :label="city.key" :key="city.key">{{city.value}}</el-checkbox>
            </el-checkbox-group>
          </el-form-item>
          <el-row>
          <el-col :span="8">
          <el-form-item prop="username" label="后台账号" label-width="80px">
            <el-input v-model="demo.username" @blur="EmailValidate" icon="search" name="title" placeholder="xxx@zhonglongedu.com"></el-input>
          </el-form-item>
        </el-col>
        <el-col :span="8">
          <el-form-item prop="password" label="密码" label-width="80px">
            <el-input v-model="demo.password" icon="search" name="title" placeholder="123456" @blur="passwordTest"></el-input>
          </el-form-item>
        </el-col>
        </el-row>
      </el-form>
       <el-button type="primary" @click="ServiceManagementAdd('demo')" style="margin-top: 8px;margin-left: 20px;">创建</el-button>
  </div>
</template>

<script>
import { fetchList } from "@/api/article";
import axios from "axios";
import api from "@/api/agent";
import { Message } from "element-ui";
// import validate from 'validate'
const cityOptions = [];
export default {
  name: "InlineEditTable",
  data() {
    return {
      rules: {
        company: [{ required: true, message: "不能为空", trigger: "blur" }],
        displayName: [{ required: true, message: "不能为空", trigger: "blur" }],
        cellphone: [{ required: true, message: "不能为空", trigger: "blur" }],
        identityCard: [
          { required: true, message: "不能为空", trigger: "blur" }
        ],
        province: [{ required: true, message: "不能为空", trigger: "change" }],
        city: [{ required: true, message: "不能为空", trigger: "change" }],
        activationMax: [
          { required: true, message: "不能为空", trigger: "blur" }
        ],
        years: [{ required: true, message: "不能为空", trigger: "blur" }],
        checkedCities: [
          { required: true, message: "不能为空", trigger: "change" }
        ],
        username: [{ required: true, message: "不能为空", trigger: "blur" }]
      },
      district: "",
      demo: {
        name: "",
        company: "",
        packageName: "",
        region: "",
        password: "123456",
        checkedCities: [],
        ord: "代理商",
        cellphone: "",
        identityCard: "",
        displayName: "",
        activationMax: "",
        years: "",
        username: ""
      },
      years: [
        {
          value: "1",
          label: "1年"
        },
        {
          value: "2",
          label: "2年"
        },
        {
          value: "3",
          label: "3年"
        },
        {
          value: "4",
          label: "4年"
        },
        {
          value: "5",
          label: "5年"
        }
      ],
      list: [],
      listLoading: true,
      listQuery: {
        page: 1,
        limit: 10
      },
      value: "",
      fileData: [],
      options: [],
      county: [],
      citys: [],
      checkAll: false,
      checkedCities: [],
      cities: cityOptions,
      isIndeterminate: true,
      checkgroup: [],
      EmailCheck: false
    };
  },
  created() {},
  mounted() {
    api.regions().then(response => {
      // this.options = response.data
      response.map((item, index) => {
        this.options.push({
          label: item.label,
          value: item.code
        });
      });
    }),
      api.scheme().then(response => {
        cityOptions.splice(0, cityOptions.length);
        response.map((item, index) => {
          cityOptions.push({
            key: item.oid,
            value: item.name
          });
        });
      });
  },
  methods: {
    EmailValidate() {
      let EmailData = this.demo.username;
      var reg = /^[A-Za-z0-9\u4e00-\u9fa5]+@[a-zA-Z0-9_-]+(\.[a-zA-Z0-9_-]+)+$/;
      if (!reg.test(EmailData)) {
        this.$message({
          showClose: true,
          message: "请按着规则XXX@zhanglongdu.com填写!",
          type: "error"
        });
        this.EmailCheck = false;
      } else {
        this.EmailCheck = true;
      }
    },
    passwordTest() {
      let passwordData = this.demo.password;
      if (passwordData.length != 6) {
        this.$message({
          showClose: true,
          message: "密码为6位数字,请重新输入!",
          type: "error"
        });
      }
    },
    identityCardTest() {
      let identityCardData = this.demo.identityCard;
      if (identityCardData.length != 18) {
        this.$message({
          showClose: true,
          message: "身份证必须18位,请重新输入!",
          type: "error"
        });
      }
    },
    cellphoneTest() {
      let cellphoneData = this.demo.cellphone;
      if (cellphoneData.length != 11) {
        this.$message({
          showClose: true,
          message: "手机号必须11位,请重新输入!",
          type: "error"
        });
      }
    },
    ServiceManagementAdd(demo) {
      this.$refs["demo"].validate(valid => {
        if (valid) {
          let checkgroup = [];
          this.demo.checkedCities.map((item, index) => {
            checkgroup.push({ oid: item });
          });
          let data = {
            activationMax: this.demo.activationMax,
            company: this.demo.company,
            displayName: this.demo.displayName,
            cellphone: this.demo.cellphone,
            identityCard: this.demo.identityCard,
            province: {
              code: this.demo.province
            },
            city: {
              code: this.demo.city
            },
            schemes: checkgroup,
            monthCount: this.demo.years,
            username: this.demo.username,
            password: this.demo.password
          };
          let passwordLength = this.demo.password;
          let cellphoneLength = this.demo.cellphone;
          console.log(this.demo.cellphone);
          let identityCardLength = this.demo.identityCard;
          if (passwordLength.length == 6) {
            if (cellphoneLength.length == 11) {
              if (identityCardLength.length == 18) {
                if (this.EmailCheck) {
                  api.AgentAdd(data).then(response => {
                    if (!response.message) {
                      this.$message({
                        showClose: true,
                        message: "添加成功！",
                        type: "success"
                      });
                      this.$router.push("/AgentManagement");
                    } else {
                      this.$message({
                        showClose: true,
                        message: response.message,
                        type: "error"
                      });
                    }
                  });
                } else {
                  this.$message({
                    showClose: true,
                    message: "请按着规则XXX@zhanglongedu.com填写!",
                    type: "error"
                  });
                  return false;
                }
              } else {
                this.$message({
                  showClose: true,
                  message: "身份证必须18位,请重新输入!",
                  type: "error"
                });
                return false;
              }
            } else {
              this.$message({
                showClose: true,
                message: "手机号必须11位,请重新输入!",
                type: "error"
              });
              return false;
            }
          } else {
            this.$message({
              showClose: true,
              message: "密码为6位数字,请重新输入!",
              type: "error"
            });
            return false;
          }
        } else {
          console.log("error submit!!");
          return false;
        }
      });
    },
    cityShow(value, data) {
      api.city(value, data).then(response => {
        if (this.citys.length > 0) {
          this.citys = [];
          this.demo.city = "请选择";
        }
        if (this.county.length > 0) {
          this.county = [];
          this.demo.county = "请选择";
        }
        response.map((item, index) => {
          this.citys.push({
            label: item.label,
            value: item.code
          });
        });
      });
    },
    countyShow(value, data) {
      api.county(value, data).then(response => {
        if (this.county.length > 0) {
          this.county = [];
          this.demo.county = "请选择";
        }
        response.map((item, index) => {
          this.county.push({
            label: item.label,
            value: item.code
          });
        });
      });
    }
  }
};
</script>

<style scoped>
.edit-input {
  padding-right: 100px;
}
#upClass {
  width: 420px;
  height: 35px;
  -webkit-appearance: none;
  background-color: #fff;
  background-image: none;
  border-radius: 4px;
  border: 1px solid #dcdfe6;
  -webkit-box-sizing: border-box;
  box-sizing: border-box;
  color: #606266;
  display: inline-block;
  font-size: inherit;
  line-height: 40px;
  outline: 0;
  padding: 0 15px;
  -webkit-transition: border-color 0.2s cubic-bezier(0.645, 0.045, 0.355, 1);
  transition: border-color 0.2s cubic-bezier(0.645, 0.045, 0.355, 1);
  width: 100%;
}
.cancel-btn {
  position: absolute;
  right: 15px;
  top: 10px;
}
</style>
